package com.summer.shopping.controller;

import com.summer.shopping.domain.table.SecondaryCategory;
import com.summer.shopping.domain.table.dto.SecondaryDTO;
import com.summer.shopping.service.SecondaryCgService;
import com.summer.shopping.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:
 * Package:
 * Description:二级分类
 *
 * @Author: 陈三岁
 * @Create
 * @Version
 */
@RestController
@Slf4j
@RequestMapping("/secondary")
public class SecondaryCgController {

    @Autowired
    private SecondaryCgService secondaryCgService;

    /**
     * 二级分类名称查询
     * 从前端传回一级分类id，作为parentId
     * @param parentId
     * @return
     */
    @GetMapping("/{parentId}")
    public Result<List<SecondaryCategory>> secondaryShowByCgId(@PathVariable Long parentId){
        log.info("查询二级分类");
        List<SecondaryCategory> secondaryList = secondaryCgService.selectByCgId(parentId);
        return Result.ok(secondaryList);
    }

    /**
     * 新增二级分类
     * @param secondaryDTO
     * @return
     */
    @PostMapping
    public Result saveSCg(@RequestBody SecondaryDTO secondaryDTO){

        secondaryCgService.saveSCg(secondaryDTO);

        return Result.ok("新增二级分类成功");
    }

    @DeleteMapping
    public Result deleteById(@RequestParam Long id){
        secondaryCgService.deleteById(id);
        return Result.ok("二级分类删除成功");
    }


    @GetMapping("/id/{id}")
    public Result<SecondaryCategory> getById(@PathVariable Long id){

        SecondaryCategory secondaryCategory = secondaryCgService.getById(id);

        return Result.ok(secondaryCategory);
    }

    @PutMapping
    public Result updateSecondaryCg(@RequestParam SecondaryCategory secondaryCategory){
        secondaryCgService.updateById(secondaryCategory);
        return Result.ok("更新成功");
    }



}
